/**
 * MoteAppCustomActivity.java
 *
 * Created by Andrew Moss on 3 Jan 2011
 *
 * Description
 */
package com.mossdev.android.moteapp;

import com.mossdev.android.moteapp.comms.ServerConnectionException;
import com.mossdev.android.moteapp.listeners.ButtonOnClickListener;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author Andrew Moss
 *
 */
public class MoteAppCustomActivity extends Activity {
	//Globals
	//---------------------------------------
	
	private int button_to_edit = 0;
	
	//Listeners
	//---------------------------------------
	
	//Send custom command button
	private OnClickListener customcmd_clicklisten = new OnClickListener() {
		public void onClick(View v) {
			//Get cmd from user
			String cmd = ((TextView)findViewById(R.id.EditText01)).getText().toString();
			if (cmd.length() < 0) {
				Log.w(Config.LogTag, "User tried to send a zero length cmd.");
				return;
			}
			try {
				MoteApp.servercomms.sendCommand(cmd);
			} catch (ServerConnectionException e) {
				Log.e(Config.LogTag, e.err_msg);
			}
		}
	};
	
	//Edit Command button
	private OnClickListener customeditok_clicklisten = new OnClickListener() {
		public void onClick(View v) {
			//Get cmd from user
			String cmd = ((TextView)findViewById(R.id.EditTextCustomButtonCommand)).getText().toString();
			if (cmd.length() < 0) {
				Log.w(Config.LogTag, "User tried to set a zero length cmd.");
			} else {
				switch (button_to_edit) {
				case 1:
					MoteApp.config.setCustomButton1(cmd);
					break;
				case 2:
					MoteApp.config.setCustomButton2(cmd);
					break;
				case 3:
					MoteApp.config.setCustomButton3(cmd);
					break;
				case 4:
					MoteApp.config.setCustomButton4(cmd);
					break;
				case 5:
					MoteApp.config.setCustomButton5(cmd);
					break;
				case 6:
					MoteApp.config.setCustomButton6(cmd);
					break;
				default:
					Log.e(Config.LogTag, "Tried to edit non-existent button.");
				break;
				}
			}
			disableEditCustomButton();
			updateCommandButtons();
		}
	};
	
	//Cancel Edit Command button
	private OnClickListener customeditcancel_clicklisten = new OnClickListener() {
	    public void onClick(View v) {
	      disableEditCustomButton();
	    }
	};
	
	
	//Methods
	//---------------------------------------
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.custom);

		// Set up Buttons
		Button button = (Button) findViewById(R.id.ButtonCustomCmd);
		button.setOnClickListener(customcmd_clicklisten);
		button = (Button) findViewById(R.id.ButtonEditCancel);
		button.setOnClickListener(customeditcancel_clicklisten);
		button = (Button) findViewById(R.id.ButtonEditOk);
		button.setOnClickListener(customeditok_clicklisten);
		
		button = (Button)findViewById(R.id.ButtonCustom1);
		registerForContextMenu(button);
        button = (Button)findViewById(R.id.ButtonCustom2);
        registerForContextMenu(button);
        button = (Button)findViewById(R.id.ButtonCustom3);
        registerForContextMenu(button);
        button = (Button)findViewById(R.id.ButtonCustom4);
        registerForContextMenu(button);
        button = (Button)findViewById(R.id.ButtonCustom5);
        registerForContextMenu(button);
        button = (Button)findViewById(R.id.ButtonCustom6);
        registerForContextMenu(button);
    	updateCommandButtons();

	}
    
    /**
     * Called when user long clicks a view in the activity
     */
    @Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Custom Buttons");
		String menutext = "Edit Button ";
		switch (v.getId()) {
		case R.id.ButtonCustom1:
			menutext = menutext + "1";
			break;
		case R.id.ButtonCustom2:
			menutext = menutext + "2";
			break;
		case R.id.ButtonCustom3:
			menutext = menutext + "3";
			break;
		case R.id.ButtonCustom4:
			menutext = menutext + "4";
			break;
		case R.id.ButtonCustom5:
			menutext = menutext + "5";
			break;
		case R.id.ButtonCustom6:
			menutext = menutext + "6";
			break;
		default:
			menutext = menutext + "?";
			break;
		}
		
		menu.add(0, v.getId(), 0, menutext);
	}
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.ButtonCustom1:
    	  this.button_to_edit = 1;
    	  break;
      case R.id.ButtonCustom2:
    	  this.button_to_edit = 2;
    	  break;
      case R.id.ButtonCustom3:
    	  this.button_to_edit = 3;
    	  break;
      case R.id.ButtonCustom4:
    	  this.button_to_edit = 4;
    	  break;
      case R.id.ButtonCustom5:
    	  this.button_to_edit = 5;
    	  break;
      case R.id.ButtonCustom6:
    	  this.button_to_edit = 6;
    	  break;
      default:
        return super.onContextItemSelected(item);
      }
      enableEditCustomButton();
      return true;
    }
    
    /**
     * The tabbed activity is brought to the foreground.
     */
	@Override
    protected void onResume()
    {
		super.onResume();
		updateCommandButtons();
    }
    
    //Private Methods
	//---------------------------------------
    
    /**
     * Get the custom button config from data store and update their actions
     * and text.
     */
    private void updateCommandButtons() {
    	 Button button = (Button)findViewById(R.id.ButtonCustom1);
         button.setOnClickListener(new ButtonOnClickListener(MoteApp.config.getCustomButton1()));
         button.setText(MoteApp.config.getCustomButton1());
         
         button = (Button)findViewById(R.id.ButtonCustom2);
         button.setOnClickListener(new ButtonOnClickListener(MoteApp.config.getCustomButton2()));
         button.setText(MoteApp.config.getCustomButton2());
         
         button = (Button)findViewById(R.id.ButtonCustom3);
         button.setOnClickListener(new ButtonOnClickListener(MoteApp.config.getCustomButton3()));
         button.setText(MoteApp.config.getCustomButton3());
         
         button = (Button)findViewById(R.id.ButtonCustom4);
         button.setOnClickListener(new ButtonOnClickListener(MoteApp.config.getCustomButton4()));
         button.setText(MoteApp.config.getCustomButton4());
         
         button = (Button)findViewById(R.id.ButtonCustom5);
         button.setOnClickListener(new ButtonOnClickListener(MoteApp.config.getCustomButton5()));
         button.setText(MoteApp.config.getCustomButton5());
         
         button = (Button)findViewById(R.id.ButtonCustom6);
         button.setOnClickListener(new ButtonOnClickListener(MoteApp.config.getCustomButton6()));
         button.setText(MoteApp.config.getCustomButton6());
    }
    
    /**
     * Show the controls for editing custom commands
     */
    private void enableEditCustomButton() {
    	((View) findViewById(R.id.EditButtonLayout)).setVisibility(View.VISIBLE);
    }
    
    /**
     * Hide controls for editing custom commands
     */
    private void disableEditCustomButton() {
    	((View) findViewById(R.id.EditButtonLayout)).setVisibility(View.INVISIBLE);
    }
    
    
}
